//
// Created by antler on 2019/1/8.
//

#include <sys/stat.h>
#include "FileUtils.h"

namespace FileUtils {
    bool getFileData(const std::string &filePath, ResizableBuffer &buf) {
        bool ret = false;
        FILE *fp = nullptr;

        do {
            fp = fopen(filePath.c_str(), "rb");
            if (!fp) {
                break;
            }

            int fd = fileno(fp);

            struct stat statBuf;
            if (fstat(fd, &statBuf) == -1) {
                break;
            }

            size_t size = statBuf.st_size;
            buf.resize(size);

            size_t readsize = fread(buf.data(), 1, size, fp);
            if (readsize < size) {
                buf.resize(readsize);
                break;
            }

            ret = true;

        } while (0);

        if (fp) {
            fclose(fp);
        }

        return ret;
    }

    bool getFileDataFromZip(const std::string &zipFilePath, const std::string &fileName,
                            std::vector<char> &buf) {
        return false;
    }

};

